package com.kaisa.activity.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;

import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;

import android.widget.ImageView;

import android.widget.TextView;

import com.kaisa.activity.R;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/12/17.
 */
public class ShipActivity extends AppCompatActivity{
    @ViewInject(R.id.tv_ship_all)
    private TextView allTV;
    @ViewInject(R.id.tv_ship_price)
    private TextView priceTV;
    @ViewInject(R.id.tv_ship_days)
    private TextView daysTV;
    @ViewInject(R.id.tv_ship_name)
    private TextView nameTV;
    @ViewInject(R.id.tv_ship_destination)
    private TextView destinationTV;


    private String[] prices = {"全部","5000元以下","5001-8000元","8001-10000元","10001-15000元","15000以上元",};
    private String[] day = {"全部","1-5天","6-9天","10-15天","16天以上",};
    private String[] naems = {"全部","皇家加勒比","MSC","丽星","歌诗达",};




    private String all;
    private String price;
    private String days;
    private String name;
    private String destination;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ship);
        x.view().inject(this);

        initView();
        initData();

    }

    private void initView() {

    }

    private void initData() {
    }


    public void click(View view) {
        switch (view.getId()) {
            case R.id.ll_ship_all:
                //Toast.makeText(this,"--",Toast.LENGTH_SHORT).show();
                Intent intent1 = new Intent(this,OriginActivity.class);
                startActivityForResult(intent1,101);
                break;
            case R.id.ll_ship_destination:
                Intent intent2 = new Intent(this,CountryActivity.class);
                startActivityForResult(intent2,102);
                break;
            case R.id.ll_ship_price:
                showPopwindow(prices, R.id.ll_ship_price, priceTV);
                break;
            case R.id.ll_ship_days:
                showPopwindow(day, R.id.ll_ship_days, daysTV);
                break;
            case R.id.ll_ship_name:
                showPopwindow(naems, R.id.ll_ship_name, nameTV);
                break;
            case R.id.tv_ship_search:
                Intent intent = new Intent(this,ShipListActivity.class);
                startActivity(intent);
                break;
            case R.id.iv_ship_back:
                this.finish();
                break;

        }
    }

    /**
     * 显示popupWindow
     */
    private void showPopwindow(String[] strs, int showId, final TextView textView) {
        List<String> list = new ArrayList<String>();
        ListView listView;
        ArrayAdapter adapter;
        list.clear();
        for (int i = 0; i < strs.length; i++) {
            list.add(strs[i]);
        }


        // 利用layoutInflater获得View
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.pop_content, null);

        // 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()

        final PopupWindow window = new PopupWindow(view,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT);

        // 设置popWindow弹出窗体可点击，这句话必须添加，并且是true
        window.setFocusable(true);


        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        window.setBackgroundDrawable(dw);


        // 设置popWindow的显示和消失动画
        window.setAnimationStyle(R.style.mypopwindow_anim_style);
        // 在底部显示
        window.showAtLocation(ShipActivity.this.findViewById(showId),
                Gravity.BOTTOM, 0, 0);

        // 这里检验popWindow里的button是否可以点击
        listView = (ListView) view.findViewById(R.id.pop_content_lv);

        adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                window.dismiss();
                textView.setText(((TextView)view).getText());
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 101:
                all = data.getStringExtra("all");
                allTV.setText(all);
                break;
            case 102:
                destination = data.getStringExtra("Country");
                destinationTV.setText(destination);
                break;
        }
    }
}
